package com.tianruan.dc.modules.userinfo.controller;

import com.tianruan.dc.common.utils.response.VResponse;
import com.tianruan.dc.modules.sys.user.entity.dto.UserDTO;
import com.tianruan.dc.modules.sys.user.utils.PageBean;
import com.tianruan.dc.modules.sys.user.utils.UserUtils;
import com.tianruan.dc.modules.userinfo.entity.User;
import com.tianruan.dc.modules.userinfo.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static com.tianruan.dc.common.constant.GlobalExceptionEnum.FAIL_TO_RREGISTER;
import static com.tianruan.dc.modules.userinfo.context.ObjectResult.getObjectResult;


@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    /*
    新增用户信息
     */
    @PostMapping("/add")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<Object> insertUser(@RequestBody User user){
        VResponse<Object> error = getObjectResult(user);
        if (error != null) return error;
        /*
        判断用户输入的用户类型
         */
        if(!Objects.equals(user.getType(), "0")&&!Objects.equals(user.getType(), "1")){
            return VResponse.error(1000,"您填写的用户类型错误！");
        }
        /*
        查看该用户是否存在
         */
        UserDTO userDTO = UserUtils.getUser();
        String id = userDTO.getId();
        Boolean check=userInfoService.checkUserId(id);
        if(check){
            user.setUserId(id);
            userInfoService.insertUser(user);
            return VResponse.success("新增用户信息成功！");
        }
        else {
            return VResponse.error(1000,"该账号不存在！请先注册！");
        }
    }

    /*
    根据id查看用户信息
     */
    @GetMapping("/{id}")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<User> findUser(@PathVariable String id){
        log.info("用户id{}",id);
        User user=userInfoService.findUser(id);
        return VResponse.success(user);
    }


    /*
    删除用户信息（即修改删除标记）,同时删除用户对应的项目信息
     */
    @DeleteMapping("/delete")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<Object> delete(@RequestBody UserDTO id) {
        log.info("用户id:{}",id);
        userInfoService.delete(id);
        return VResponse.success("成功删除该用户！");
    }

    /*
    分页模糊搜索学生信息
     */
    @GetMapping("/page/student")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher')")
    public VResponse<PageBean<User>> pageStu(
            @RequestParam(defaultValue = "1")  Integer page,
            @RequestParam(defaultValue = "10")  Integer pageSize,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "officeId",required = false) String officeId,
            @RequestParam(value = "major",required = false) String major,
            @RequestParam(value = "enrollmentYear",required = false) String enrollmentYear,
            @RequestParam(value = "email",required = false) String email,
            @RequestParam(value = "phone",required = false) String phone
    ) {
        log.info("模糊分页查询学生信息");
        PageBean<User> list = userInfoService.pageStu(page,pageSize,name,officeId,major,enrollmentYear,email,phone);
        return VResponse.success(list);
    }
    /*
    分页模糊搜索导师信息
     */
    @GetMapping("/page/teacher")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<PageBean<User>> pageTea(
            @RequestParam(defaultValue = "1")  Integer page,
            @RequestParam(defaultValue = "10")  Integer pageSize,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "college",required = false) String college,
            @RequestParam(value = "major",required = false) String major,
            @RequestParam(value = "post",required = false) String post,
            @RequestParam(value = "email",required = false) String email,
            @RequestParam(value = "phone",required = false) String phone
    ) {
        log.info("模糊分页查询老师信息{}",email);
        PageBean<User> list = userInfoService.pageTea(page,pageSize,name, post, college, phone, major, email);
        return VResponse.success(list);
    }



    /*
    后台修改用户信息
     */
    @PutMapping("/pc/update")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<Object> updateUserPC(@RequestBody User user){
        try{
            if(user.getId()==null){
                return VResponse.error(1000,"id不可为空！");
            }
            else {
                user.setUserId(user.getId());
                userInfoService.updateUser(user);
                return VResponse.success("修改用户信息成功！");
            }
        }catch (Exception e){
            return VResponse.error(1000,"修改失败！");
        }

    }
}
